#!/usr/bin/env python
# -*- coding:utf-8 -*-
from __future__ import print_function
import MySQLdb

from mysqlmoudel.sqlbuilder import MysqlBuilder


class Abssql:
    @staticmethod
    def getsqlfractry():
        return SqlFractry()


class SqlFractry(Abssql):
    @staticmethod
    def mysqlFractry(type):
        if type == 'mysql':
            return Mysql()
        assert 0, "err sql type please check:" + type


class Mysql(SqlFractry):
    def __init__(self):
        self.db = ''
        self.cursor = ''

    def link(self, mysqlmoudle):
        # print(mysqlmoudle[0], mysqlmoudle[1], mysqlmoudle[2], mysqlmoudle[3])
        self.db = MySQLdb.connect(mysqlmoudle['host'], mysqlmoudle['user'], mysqlmoudle['passwd'], mysqlmoudle['db'],
                                  mysqlmoudle['port'])
        self.cursor = self.db.cursor()
        return self.db, self.cursor

    def close(self):
        self.db.close()

class Mysql_util:
    def __init__(self):
        self.mysql = ''
        self.db = ''
        self.cursor = ''
        self.init()


    def init(self):
        self.mysql = Abssql.getsqlfractry().mysqlFractry("mysql")
        builder = MysqlBuilder('../config/mysql.ini', 'mysqllocalhost')
        moudle = builder.build_all().get_moudle().todict()
        #print(moudle)
        self.mysql.link(moudle)
        self.db = self.mysql.db
        self.cursor = self.mysql.cursor



if __name__ == "__main__":
    mysql = Abssql.getsqlfractry().mysqlFractry("mysql")
    builder = MysqlBuilder('../config/mysql.ini', 'mysqllocalhost')
    moudle = builder.build_all().get_moudle().todict()
    print(moudle)
    mysql.link(moudle)
    
